package drp.snowbell.util;

import java.sql.Types;
import java.util.Date;

import drp.snowbell.storage.Blob;
import drp.snowbell.storage.Clob;


public class TypeHelper {

	public static int toSqlTypes(DbType type) {
		int ret = 0;
		switch (type) {
		case BIGINT:
			ret = Types.BIGINT;
			break;
		case CHAR:
			ret = Types.CHAR;
			break;
		case TIMESTAMP:
			ret = Types.TIMESTAMP;
			break;
		case FLOAT:
			ret = Types.FLOAT;
			break;
		case INTEGER:
			ret = Types.INTEGER;
			break;
		case NCHAR:
			ret = Types.NCHAR;
			break;
		case CLOB:
			ret = Types.CLOB;
			break;
		case NVARCHAR:
			ret = Types.NVARCHAR;
			break;
		case DATE:
			ret = Types.DATE;
			break;
		case BLOB:
			ret = Types.BLOB;
			break;
		case TINYINT:
			ret = Types.TINYINT;
			break;
		case VARCHAR:
			ret = Types.VARCHAR;
			break;
		}
		return ret;
	}

	/**
	 * 根据BaseFieldType 转换为数据库类型
	 * 
	 * @param bft
	 * @return
	 */
	public static DbType toDbType(BaseFieldType bft) {
		DbType ret = null;
		switch (bft) {
		case Boolean:
			ret = DbType.TINYINT;
			break;
		case Byte:
			ret = DbType.TINYINT;
			break;

		case Character:
			ret = DbType.TINYINT;
			break;

		case Double:
			ret = DbType.FLOAT;
			break;

		case Float:
			ret = DbType.FLOAT;
			break;

		case Integer:
			ret = DbType.INTEGER;
			break;

		case Long:
			ret = DbType.BIGINT;
			break;

		case Short:
			ret = DbType.INTEGER;
			break;

		case String:
			ret = DbType.VARCHAR;
			break;
		case Date:
			ret = DbType.DATE;
			break;
		case Blob:
			ret = DbType.BLOB;
			break;
		case Clob:
			ret = DbType.CLOB;
			break;
		default:
		case Object:
			ret = DbType.VARCHAR;
		}
		return ret;
	}

	/**
	 * 根据class 获取数据库类型
	 * 
	 * @param clazz
	 * @return
	 */
	public static DbType getDbType(Class<? extends Object> clazz) {
		BaseFieldType bft = getType(clazz);
		return toDbType(bft);
	}

	public static BaseFieldType getType(Class<? extends Object> clazz) {
		BaseFieldType ret;
		if (clazz == String.class) {
			ret = BaseFieldType.String;
		} else if (clazz == Integer.class || clazz == int.class) {
			ret = BaseFieldType.Integer;
		} else if (clazz == Long.class || clazz == long.class) {
			ret = BaseFieldType.Long;
		} else if (clazz == Double.class || clazz == double.class) {
			ret = BaseFieldType.Double;
		} else if (clazz == Float.class || clazz == float.class) {
			ret = BaseFieldType.Float;
		} else if (clazz == Byte.class || clazz == byte.class) {
			ret = BaseFieldType.Byte;
		} else if (clazz == Character.class || clazz == char.class) {
			ret = BaseFieldType.Character;
		} else if (clazz == Boolean.class || clazz == boolean.class) {
			ret = BaseFieldType.Boolean;
		} else if (clazz == Short.class || clazz == short.class) {
			ret = BaseFieldType.Short;
		} else if (Date.class.isAssignableFrom(clazz)) {
			ret = BaseFieldType.Date;
		} else if (clazz == Blob.class) {
			ret = BaseFieldType.Blob;
		} else if (clazz == Clob.class) {
			ret = BaseFieldType.Clob;
		} else {
			ret = BaseFieldType.Object;
		}
		return ret;
	}

	public static BaseFieldType getType(Object obj) {
		BaseFieldType ret;
		if (obj instanceof String) {
			ret = BaseFieldType.String;
		} else if (obj instanceof Integer) {
			ret = BaseFieldType.Integer;
		} else if (obj instanceof Long) {
			ret = BaseFieldType.Long;
		} else if (obj instanceof Double) {
			ret = BaseFieldType.Double;
		} else if (obj instanceof Float) {
			ret = BaseFieldType.Float;
		} else if (obj instanceof Byte) {
			ret = BaseFieldType.Byte;
		} else if (obj instanceof Character) {
			ret = BaseFieldType.Character;
		} else if (obj instanceof Boolean) {
			ret = BaseFieldType.Boolean;
		} else if (obj instanceof Short) {
			ret = BaseFieldType.Short;
		} else if (obj instanceof Date) {
			ret = BaseFieldType.Date;
		} else if (obj instanceof Blob) {
			ret = BaseFieldType.Blob;
		} else if (obj instanceof Clob) {
			ret = BaseFieldType.Clob;
		} else {
			ret = BaseFieldType.Object;
		}
		return ret;
	}

	public static Class<? extends Object> getTypeClass(BaseFieldType theType) {
		Class<? extends Object> ret;
		switch (theType) {
		case Boolean:
			ret = Boolean.class;
			break;
		case Byte:
			ret = Byte.class;
			break;

		case Character:
			ret = Character.class;
			break;

		case Double:
			ret = Double.class;
			break;

		case Float:
			ret = Float.class;
			break;

		case Integer:
			ret = Integer.class;
			break;

		case Long:
			ret = Long.class;
			break;

		case Short:
			ret = Short.class;
			break;

		case String:
			ret = String.class;
			break;
		case Date:
			ret = Date.class;
			break;
		case Blob:
			ret = Blob.class;
			break;
		case Clob:
			ret = Clob.class;
			break;
		default:
		case Object:
			ret = null;
		}
		return ret;
	}
}
